//
//  Today_debrisContentCollectionViewCell.m
//  Time
//
//  Created by dllo on 16/7/19.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "Today_debrisContentCollectionViewCell.h"
#import "Today_debris_ContentTableViewCell.h"
#import "Today_debris_TagTableViewCell.h"

@interface Today_debrisContentCollectionViewCell ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic,retain) UITableView *debrisTableView;

@property (nonatomic,assign) BOOL isDown;

@property (nonatomic,assign) CGFloat debrisOffsetX;


@end


@implementation Today_debrisContentCollectionViewCell

-(void)dealloc {
    [_debrisModelArray release];
    [_debrisTableView release];
    _debrisTableView.delegate = nil;
    _debrisTableView.dataSource = nil;
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.debrisModelArray = [NSMutableArray arrayWithCapacity:5];
        
        self.debrisTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - 88) style:UITableViewStyleGrouped];
        [self.contentView addSubview:self.debrisTableView];
        self.debrisTableView.delegate = self;
        self.debrisTableView.dataSource = self;
        
        self.debrisTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.debrisTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.isDown = YES;
            [self.delegate loadNewDataForDebris:self.isDown];
            [self.debrisTableView.mj_header endRefreshing];
        }];
        
        self.debrisTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            self.isDown = NO;
            [self.delegate loadNewDataForDebris:self.isDown];
            [self.debrisTableView.mj_footer endRefreshing];
        }];
        
        
        
        [self.debrisTableView registerClass:[Today_debris_ContentTableViewCell class] forCellReuseIdentifier:@"today_debris_Cell"];
        
        [self.debrisTableView registerClass:[Today_debris_TagTableViewCell class] forCellReuseIdentifier:@"today_debris_Tag_Cell"];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.debrisModelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCB_Today_Debris_ContentModel *model = [self.debrisModelArray objectAtIndex:indexPath.section];
    if ([model.isTag integerValue] == 0){
        Today_debris_ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_debris_Cell" forIndexPath:indexPath];
        cell.model = [self.debrisModelArray objectAtIndex:indexPath.section];
        return cell;
    } else {
        Today_debris_TagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_debris_Tag_Cell" forIndexPath:indexPath];
        /**
         *  cell.model
         */
        cell.model = [self.debrisModelArray objectAtIndex:indexPath.section];
        cell.backgroundColor = [UIColor cyanColor];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCB_Today_Debris_ContentModel *model = [self.debrisModelArray objectAtIndex:indexPath.section];
    if([model.isTag integerValue] == 0){
        if (![model.coverimg isEqualToString:@""]){
//             if ([model.songid isEqualToString:@""]){
                return 50 + SWIDTH + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 70;
//            } else {
//                return 50 + SWIDTH + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 80 + 70;
//            }
        } else {
//            if ([model.songid isEqualToString:@""]) {
                return 50 + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 70;
//            } else {
//                return 50 + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 80 + 70;
//            }
        }
    }
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x > self.debrisOffsetX){
        
    } else {
        
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.debrisOffsetX = scrollView.contentOffset.x;
}

-(void)setDebrisModelArray:(NSMutableArray *)debrisModelArray{
    if (_debrisModelArray != debrisModelArray) {
        [_debrisModelArray release];
        _debrisModelArray = [debrisModelArray retain];
    }
    [self.debrisTableView reloadData];
}

@end
